<template>
  <!-- 危急值上报 -->
  <div id="reportAlarmValueDetail">
    <vxe-modal v-model="detailModalShow" id="myModalResult" width="80%" height="80%" min-width="460" min-height="320" destroy-on-close show-zoom transfer :z-index="1004">
      <template #title>
        <span>危急值上报</span>
      </template>

      <template #default>
        <el-descriptions :column="4" labelClassName="description-label" contentClassName="description-content" border class="margin-bottom-sm">
          <el-descriptions-item>
            <template #label>
              <span>姓名</span>
            </template>
            <span>{{ mainInfo.patNa }}</span>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>病历号</span>
            </template>
            <span>{{ mainInfo.patNo }}</span>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>性别</span>
            </template>
            <span> {{ mainInfo.sex === '1' ? '男' : mainInfo.sex === '2' ? '女' : '未知' }}</span>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>年龄</span>
            </template>
            <span>{{ mainInfo.age }}</span>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>病人类型</span>
            </template>
            <div v-for="(item, index) in patTypeList" :key="index">
              <span v-if="item.code === mainInfo.patType">{{ item.name }}</span>
            </div>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>科室</span>
            </template>
            <span>{{ mainInfo.reqDeptNa }}</span>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>样本类型</span>
            </template>
            <span>{{ mainInfo.traits }}</span>
          </el-descriptions-item>
          <el-descriptions-item>
            <template #label>
              <span>报告单元</span>
            </template>
            <span>{{ mainInfo.rptGroupNa }}</span>
          </el-descriptions-item>
        </el-descriptions>
        <div class="tableForm">
          <div class="tableBody">
            <vxe-table ref="wlResultListTable" border height="100%" :data="wlResultList" :row-config="{ isCurrent: true, isHover: true }" :keyboard-config="{
                isArrow: true
              }" :radio-config="{ highlight: true }" resizable show-overflow>
              <vxe-column type="checkbox" width="30" align="left"></vxe-column>
              <vxe-column type="seq" title="序号" width="30" align="left"></vxe-column>
              <vxe-column field="isReport" width="90" title="是否通报" align="left">
                <template #default="{ row }">
                  <vxe-switch v-model="row.isReport" size="mini" open-label="是" close-label="否" open-value="1" close-value="0"></vxe-switch>
                </template>
              </vxe-column>
              <vxe-column title="项目名称" width="150" align="left">
                <template #default="{ row }">
                  <span>{{ row.itemNo + ' ' + row.itemNa }}</span>
                </template>
              </vxe-column>
              <vxe-column field="testResult" title="检验结果" width="150" align="left"></vxe-column>
              <vxe-column field="resultFlag" title="结果标志" width="150" align="left">
                <template #default="{ row }">
                  <div v-for="(item, index) in resultflags" :key="index">
                    <span v-if="item.code === row.resultFlag">{{ item.name }}</span>
                  </div>
                </template>
              </vxe-column>
              <vxe-column field="ref" title="参考范围" width="150" align="left"> </vxe-column>
              <vxe-column field="exaAlarmValueLevel" title="危急值信息" min-width="30" show-overflow>
                <template #default="{ row }">
                  <span> {{ row.alarmText }}</span>
                </template>
              </vxe-column>
              <vxe-column field="measure" title="处理措施" align="left" width="150">
                <template #default="{ row }">
                  <vxe-select v-model="row.measure" placeholder="" clearable transfer class-name="width-100-percentage">
                    <vxe-option v-for="(item, index) in measureList" :key="index" :label="item.name" :value="item.code"></vxe-option>
                  </vxe-select>
                </template>
              </vxe-column>
            </vxe-table>
          </div>
          <div class="formBody">
            <!-- <vxe-form
              ref="detailForm"
              :data="detailForm"
              :title-width="80"
              title-colon
              title-align="right"
              class-name="alarmValueResultForm margin-top-sm"
            >
              <vxe-form-item title="被 通 知 人" span="10" field="notifyPerson" :item-render="{}">
                <el-color-picker v-model="color1" />
              </vxe-form-item>

              <vxe-form-item title="上 报 人 员" span="10" field="reportPerson">
                <SelectDataList
                  ref="employeeSelectRef"
                  :url="'/api/sys/hisEmployee/getEmployeePageList'"
                  :displayField="[
                    { title: '编号', props: 'code' }, //列标题
                    { title: '名称', props: 'name' }
                  ]"
                  :searchParam="{
                    //seachValue: form.hisEmployeeCode
                  }"
                  :inputKeyName="'seachValue'"
                  :isPage="true"
                  :title="'点击选择员工'"
                  @selectDataRow="SelectRowClick"
                  @clearValue="clearValueHandler"
                  @sliceValue="sliceHandler"
                  v-model="detailForm.reportPerson"
                  :inputWidth="'100%'"
                />
              </vxe-form-item>
              <vxe-form-item title="申 请 单 号" span="10" field="requestNo" :item-render="{}">
                <vxe-input
                  v-model="detailForm.requestNo"
                  placeholder="请输入申请单号"
                  clearable
                ></vxe-input>
              </vxe-form-item>
              <vxe-form-item title="备 注" span="10" field="remark" :item-render="{}">
                <vxe-input v-model="detailForm.remark" clearable></vxe-input>
              </vxe-form-item>
              <vxe-form-item title="上 报 时 间" span="10" field="reportTime" :item-render="{}">
                <vxe-input
                  v-model="detailForm.reportTime"
                  placeholder="选择上报时间"
                  type="datetime"
                  transfer
                ></vxe-input>
              </vxe-form-item>
              <vxe-form-item span="24" align="center" :item-render="{}" class-name=" margin-top-sm">
                <template #default>
                  <vxe-button status="primary" content="上报" @click="save"></vxe-button>
                  <vxe-button content="取消" @click="detailModalShow = false"></vxe-button>
                </template>
              </vxe-form-item>
            </vxe-form> -->
            <el-form :model="detailForm" label-width="120px" style="margin-top: 10px">
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="被 通 知 人">
                    <el-input v-model="detailForm.notifyPerson" placeholder="请输入被通知人"> </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="上 报 人 员">
                    <SelectDataList ref="employeeSelectRef" :url="'/api/sys/hisEmployee/getEmployeePageList'" :displayField="[
                        { title: '编号', props: 'code' }, //列标题
                        { title: '名称', props: 'name' }
                      ]" :searchParam="{
                        //seachValue: form.hisEmployeeCode
                      }" :inputKeyName="'seachValue'" :isPage="true" :title="'点击选择员工'" @selectDataRow="SelectRowClick" @clearValue="clearValueHandler" @sliceValue="sliceHandler" v-model="detailForm.reportPerson" :inputWidth="'100%'" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="申 请 单 号">
                    <el-input v-model="detailForm.requestNo" placeholder="请输入申请单号"> </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="备 注">
                    <el-input v-model="detailForm.remark" placeholder="请输入备 注"> </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="上 报 时 间">
                    <el-date-picker style="width: 100%" :disabled="!allowEditRptTime" v-model="detailForm.reportTime" type="datetime" placeholder="选择上报时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
                  </el-form-item>
                </el-col>
              </el-row>
              <div style="width：100%;text-align:center">
                <el-button type="primary" @click="save" :disabled='resultIsDisabled()'>{{mainInfo.isChk}}上报</el-button>
                <el-button @click="detailModalShow = false">取消</el-button>
              </div>
            </el-form>
          </div>
        </div>
      </template>
    </vxe-modal>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref, defineExpose, getCurrentInstance } from 'vue';
import { IsEmpty, sortByField, formatDateTime } from '@/utils/common';
import SelectDataList from '@/components/SelectDataList.vue';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { getOptionValueByCodeGlobalOption, getOptionValueByCodeRptGroupOption } from '@/utils/SysOptionsHelper';

let { proxy }: any = getCurrentInstance();

let detailModalShow = ref(false); //危急值具体上报弹框
let mainInfo = reactive({}); //主记录病人信息
let wlResultList = reactive([]); //结果
let measureList = reactive([]); //字典表:处置措施
let patTypeList = reactive([]); //字典表:病人类型
let resultflags: any = reactive([]); //字典表：结果标志 resultflag
let detailForm = reactive({
  notifyPerson: '', //被通知人
  reportPerson: '', //上报人员编号
  requestNo: '', //申请单号
  remark: '', //备注
  reportTime: formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss')
});

let allowEditRptTime = ref(false);/**允许编辑上报时间 */

defineExpose({
  criticaReportData
});
function criticaReportData(mainInfoData: any) {
  mainInfo = mainInfoData;
  detailModalShow.value = true;
  detailForm.notifyPerson = '';
  detailForm.remark = '';
  detailForm.reportPerson = '';
  detailForm.reportTime = formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss');
  detailForm.requestNo = '';
  /**系统选项控制 */
  //getOptionValueByCodeGlobalOption

  const a = getOptionValueByCodeGlobalOption('wjzsbmrjsrwsjys');/**危急值上报默认接收人为送检医生 */
  if (a && (a === '1' || a === 1)) {
    if (!IsEmpty(mainInfoData.reqDoctor)) {
      detailForm.notifyPerson = mainInfoData.reqDoctor;
    }
  }

  const b = getOptionValueByCodeGlobalOption('wjzsbmrsbry');/**危急值上报默认上报人员 */
  if (b && (b === '0' || b === 0)) {
    if (!IsEmpty(mainInfoData.testDoctor)) {
      detailForm.reportPerson = mainInfoData.testDoctor;
    }
  }
  if (b && (b === '1' || b === 1)) {
    if (!IsEmpty(mainInfoData.chkDoctor)) {
      detailForm.reportPerson = mainInfoData.chkDoctor;
    }
  }

  const c = getOptionValueByCodeGlobalOption('yxxgwjzsbsj');/**允许修改危急值上报时间 */
  if (a && (c === '1' || c === 1)) {
    allowEditRptTime.value = true;
  }


  getDictMeasureList();
  getDictPatTypeList();
  getAlarmResultData();
  getDictDetailList();
}

//获取是危急值的检验结果
function getAlarmResultData(callback) {
  wlResultList.length = 0;
  const url = '/lab/rptResult/getAlarmResult';
  proxy.$http
    .get(url, { params: { rptGroup:mainInfo.rptGroup,sampleda: mainInfo.sampleda ,sampleNo:mainInfo.sampleNo}})
    .then((res: any) => {
      wlResultList.push(...res.data);
      if (callback) {
        callback();
      }
    })
    .catch((err) => {
      VxeMessage(err.data.error.message, VxeNotifyType.error);
    });
}
//上报
function save() {
  const $table = proxy.$refs.wlResultListTable as any;
  const selectRows = $table.getCheckboxRecords();
  if (selectRows.length <= 0) {
    proxy.$XModal.message({ content: '还未选择需要上报的数据', status: 'warning' });
    return;
  }
  const saveData = [];
  selectRows.forEach((item, idx) => {
    const data = {};
    data.rptGroup = item.rptGroup;
    data.sampleda = item.sampleda;
    data.sampleNo = item.sampleNo;
    data.resultId = item.id;
    data.isReport = IsEmpty(item.isReport) ? 0 : item.isReport;
    data.measure = IsEmpty(item.measure) ? null : item.measure;
    data.notifyPerson = IsEmpty(detailForm.notifyPerson) ? null : detailForm.notifyPerson;
    data.reportPerson = IsEmpty(detailForm.reportPerson) ? null : detailForm.reportPerson;
    data.requestNo = IsEmpty(detailForm.requestNo) ? null : detailForm.requestNo;
    data.remark = IsEmpty(detailForm.remark) ? null : detailForm.remark;
    data.reportTime = IsEmpty(detailForm.reportTime) ? null : detailForm.reportTime;
    saveData.push(data);
  });
  const url = '/lab/alarmReport/batchAddAlarmReportList';
  proxy.$http
    .post(url, saveData)
    .then((res: any) => {
      getAlarmResultData(() => {
        if (wlResultList && wlResultList.length <= 0) {
          proxy.$emit('treatFinish');
          detailModalShow.value = false;
        }
      }); // 获取是危急值的结果信息
      VxeMessage(res.status.msg, VxeNotifyType.success);
    })
    .catch((err: any) => {
      VxeMessage(err, VxeNotifyType.error);
    });
}

// 点击组件表格行
function SelectRowClick(row) {
  detailForm.reportPerson = row.code;
}
// 清空组件输入框
function clearValueHandler() {
  detailForm.reportPerson = '';
}
// 键盘事件删除输入框文字
function sliceHandler() {
  let newStr = detailForm.reportPerson.slice(0, -1);
  detailForm.reportPerson = newStr;
}

//获取处理措施数据字典
function getDictMeasureList() {
  measureList.length = 0;
  const url = '/api/sys/basDictDetail/findDictDetailList';
  proxy.$http
    .get(url, { params: { code: 'measure' } })
    .then((res: any) => {
      measureList.push(...res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}
//获取病人类型数据字典
function getDictPatTypeList() {
  patTypeList.length = 0;
  const url = '/api/sys/basDictDetail/findDictDetailList';
  proxy.$http
    .get(url, { params: { code: 'PATTYPE' } })
    .then((res: any) => {
      patTypeList.push(...res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}
function getDictDetailList() {
  resultflags.length = 0;
  const url = '/api/sys/basDictDetail/findDictDetailList';
  proxy.$http
    .get(url, { params: { code: 'resultFlag' } })
    .then((res: any) => {
      resultflags.push(...res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}
/**判断是否审核，若审核范围ture */
function resultIsDisabled() {
  if (!IsEmpty(mainInfo) && mainInfo.isChk == '2') {
    return true;
  } else {
    return false;
  }
}
</script>

<style lang="scss" scoped>
:deep(.vxe-modal--wrapper) {
  z-index: 10 !important;
}
:deep(.el-descriptions__label) {
  width: 120px;
}
:deep(.el-descriptions__content) {
  width: 400px;
  color: #000;
}
:deep(.vxe-cell) {
  padding-left: 5px;
}
.tableForm {
  height: calc(100% - 80px);
  .tableBody {
    height: 75%;
  }
  .formBody {
    height: 25%;
    padding: 0 25px;
    :deep(.vxe-form--item) {
      margin-right: 15px;
    }
  }
}
</style>
<style lang="scss">
.alarmValueResultForm.vxe-form {
  .vxe-switch {
    padding: 0;
  }
}
</style>
